package edu.bu.cs673.superkids123.parents.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import javax.swing.JTextArea;
import javax.swing.JTextField;

import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.AbstractNavigationView;
import edu.bu.cs673.superkids123.children.model.Theme;
import edu.bu.cs673.superkids123.common.model.Child;

import edu.bu.cs673.superkids123.common.model.User;
import edu.bu.cs673.superkids123.parents.controller.EditChildController;

public class EditChildView extends AbstractNavigationView implements KeyListener,
		ActionListener {
	
	// TODO: Pull magic numbers out the center of programs with constants like this
	private static final int[] COLUMN_WIDTHS = new int[] { 100, 200, 0, 194, 141, 140, 300, 0 };
	private static final int[] ROW_HEIGHTS = new int[] { 0, 50, 50, 50, 50, 0 };
	private static final double[] COLUMN_WEIGHTS = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
	private static final double[] ROW_WEIGHTS = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
	private static final Dimension INFO_DIMENSION = new Dimension(600, 200);
	
	private JTextField nameField;
	private JButton editBtn;
	private JButton deleteBtn;
	private JButton addBtn;
	private JLabel errorMessage;
	JComboBox kidsComboBox;
	List<Child> children;

	// Start with false as these will be empty
	private boolean userNamePassed = false;

	public EditChildView(AbstractFrame mainFrame) {
		super(mainFrame);
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	protected JPanel layout() {
		JPanel p = new JPanel();
		p.setSize(800, 600);
		p.setBackground(Color.WHITE);
		p.setLayout(new BorderLayout());

		JPanel info = new JPanel();
		info.setLayout(new BorderLayout(0, 0));
		p.add(info, BorderLayout.CENTER);

		JPanel userInformation = new JPanel();
		GridBagLayout gblUserInfo = new GridBagLayout();
		gblUserInfo.columnWidths = COLUMN_WIDTHS;
		gblUserInfo.rowHeights = ROW_HEIGHTS;
		gblUserInfo.columnWeights = COLUMN_WEIGHTS;
		gblUserInfo.rowWeights = ROW_WEIGHTS;
		userInformation.setLayout(gblUserInfo);

		/*JTextArea infoMsg = new JTextArea();
		GridBagConstraints gbcInfo = new GridBagConstraints();
		gbcInfo.gridwidth = 6;
		gbcInfo.insets = new Insets(0, 0, 5, 5);
		gbcInfo.fill = 1;
		gbcInfo.gridx = 1;
		gbcInfo.gridy = 0;
		userInformation.add(infoMsg, gbcInfo);
		infoMsg.setLineWrap(true);
		infoMsg.setWrapStyleWord(true);
		infoMsg.setFont(Theme.PARENTS_FONT);
		infoMsg.setBackground(Color.WHITE);
		infoMsg.setEditable(false);
		infoMsg.setSize(INFO_DIMENSION);
		infoMsg.setText("	Add, edit or delete child");*/
		
		nameField = new JTextField();
		nameField.setFont(Theme.PARENTS_FONT);
		nameField.addKeyListener(this);
		
		JTextField yearField = new JTextField();
		yearField.setFont(Theme.PARENTS_FONT);
		yearField.addKeyListener(this);
				
		/*JButton btnAddChild = new JButton("Add child");
		btnAddChild.setBackground(Color.DARK_GRAY);
		btnAddChild.setEnabled(false);
		GridBagConstraints gbc_btnAddChild = new GridBagConstraints();
		gbc_btnAddChild.insets = new Insets(0, 0, 5, 5);
		gbc_btnAddChild.gridx = 4;
		gbc_btnAddChild.gridy = 1;
		userInformation.add(btnAddChild, gbc_btnAddChild);*/

		// Child Name
		JLabel kidLabel = new JLabel("Select kid:");
		kidLabel.setFont(Theme.PARENTS_FONT);
		GridBagConstraints gbcNameLabel = new GridBagConstraints();
		gbcNameLabel.insets = new Insets(0, 0, 5, 5);
		gbcNameLabel.anchor = GridBagConstraints.EAST;
		gbcNameLabel.gridx = 1;
		gbcNameLabel.gridy = 2;
		userInformation.add(kidLabel, gbcNameLabel);

		List<String> kids = new ArrayList<String>();
		// TODO: Call a controller, not the data object directly from the view
		children = getMainFrame().getController(EditChildController.class).getAllChildren();
		Iterator<Child> it = children.iterator();
		Child c = new Child();
		while (it.hasNext())
		{
			c = it.next();
			kids.add(c.getName());
		}
		String[] kids2 = new String[kids.size()]; 
		kids2 = kids.toArray(kids2);
		kidsComboBox = new JComboBox(kids2);
		//kidsComboBox.setModel(new DefaultComboBoxModel(kids.toArray(new String[3])));
		GridBagConstraints gbcKidsComboBox = new GridBagConstraints();
		gbcKidsComboBox.insets = new Insets(0, 0, 5, 5);
		gbcKidsComboBox.gridx = 3;
		gbcKidsComboBox.gridy = 2;
		userInformation.add(kidsComboBox, gbcKidsComboBox);
		System.out.println(kidsComboBox);
		
		editBtn = new JButton("Edit");
		GridBagConstraints gbc_editBtn = new GridBagConstraints();
		gbc_editBtn.insets = new Insets(0, 0, 5, 5);
		gbc_editBtn.gridx = 4;
		gbc_editBtn.gridy = 2;
		userInformation.add(editBtn, gbc_editBtn);
		editBtn.setEnabled(true);
		editBtn.addActionListener(this);
		
		deleteBtn = new JButton("Delete");
		// TODO: Make this action part of the class action listener instead of doing anonymously here
		deleteBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		
		deleteBtn.setEnabled(true);
		GridBagConstraints gbc_btnDelete = new GridBagConstraints();
		gbc_btnDelete.insets = new Insets(0, 0, 5, 5);
		gbc_btnDelete.gridx = 5;
		gbc_btnDelete.gridy = 2;
		userInformation.add(deleteBtn, gbc_btnDelete);
		deleteBtn.addActionListener(this);
		
		GridBagConstraints year_textField = new GridBagConstraints();
		year_textField.fill = GridBagConstraints.HORIZONTAL;
		year_textField.insets = new Insets(0, 0, 5, 0);
		year_textField.gridx = 7;
		year_textField.gridy = 2;
		
		// TODO: What is the yearField for? It doesn't appear to be used.
		userInformation.add(yearField, year_textField);
		yearField.setColumns(4);

		// TODO: What is this createPanel for?
		JPanel createPanel = new JPanel(new BorderLayout());
		createPanel.setBackground(Color.WHITE);

		JLabel avatarLabel = new JLabel("Avatar:");
		avatarLabel.setFont(Theme.PARENTS_FONT);
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.anchor = GridBagConstraints.EAST;
		gbc_label.insets = new Insets(0, 0, 5, 5);
		gbc_label.gridx = 1;
		gbc_label.gridy = 3;

		errorMessage = new JLabel("");
		errorMessage.setForeground(Color.RED);
		errorMessage.setBackground(Color.WHITE);
		createPanel.add(errorMessage, BorderLayout.WEST);

		GridBagConstraints gbcCreatePanel = new GridBagConstraints();
		gbcCreatePanel.insets = new Insets(0, 0, 0, 5);
		gbcCreatePanel.anchor = GridBagConstraints.EAST;
		gbcCreatePanel.gridx = 6;
		gbcCreatePanel.gridy = 5;
		userInformation.add(createPanel, gbcCreatePanel);

		info.add(userInformation, BorderLayout.NORTH);

		return p;
	}

	// TODO: Set all of your actionlisteners to work from here.
	@Override
	public void actionPerformed(ActionEvent event) {
		
		if(event.getSource() == deleteBtn){
			Child child=children.get(kidsComboBox.getSelectedIndex());
			getMainFrame().getController(EditChildController.class).deleteUser(
					child);
		}
		
		if (event.getSource() == editBtn) {
			Child child=children.get(kidsComboBox.getSelectedIndex());
			getMainFrame().getController(EditChildController.class).editUser(
					child);
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// Ignore this

	}

	// TODO: This event is for JTextFields, Are you planning on using JTextFields?
	@Override
	public void keyReleased(KeyEvent e) {
		Object source = e.getSource();
		if (source.equals(nameField)) {
			checkName();
		}

		// Check values
		if (userNamePassed) {
			editBtn.setEnabled(true);
		} else {
			editBtn.setEnabled(false);
		}

	}

	private void checkName() {
		String userName = nameField.getText();
		// Make sure field isn't empty
		if (!userName.equals("")) {
			errorMessage.setText("");
			userNamePassed = true;
			// Check if user already exists
		} else {
			errorMessage.setText("Username cannot be empty!");
			userNamePassed = false;
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// Ignore this
	}
	
	@Override
	public String getTitle() {
		return "Manage Child Account";
	}

	@Override
	public String getBorderTitle() {
		return "Select Child";
	}

}
